﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ArduinoSketch.Core
{
    public class Utils
    {
        private Utils() { }


        public static bool Check(char c, char[] array)
        {
            if (array != null)
                for (int i = 0; i < array.Length; i++)
                    if (c.Equals(array[i])) return true;

            return false;
        }

        public static bool CheckNotNull(object obj)
        {
            return CheckNotNull(obj, null, false);
        }

        public static bool CheckNotNull(object obj, string name, bool throwException)
        {
            if (throwException && obj == null)
                if (name == null)
                    throw new ArgumentNullException();
                else
                    throw new ArgumentNullException(name);
            else
                return obj != null;
        }

        public static ArduinoTargetConfig LoadArduinoTargetConfig()
        {
            ArduinoTargetConfig _arduinoTarget = null;

            if (File.Exists(StringConst.ARDU_TARGET_CNFG))
            {
                _arduinoTarget = XmlSerializable.ReadFile<ArduinoTargetConfig>(StringConst.ARDU_TARGET_CNFG);
            }

            return _arduinoTarget;
        }

        public static Hardwares LoadHardwardBoard()
        {
            Hardwares _hardware = null;

            if (File.Exists(StringConst.BOARD_CNFG))
            {
                _hardware = XmlSerializable.ReadFile<Hardwares>(StringConst.BOARD_CNFG);
            }

            return _hardware;
        }

        public static HardwareProfile GetHardwareProfile(string profileName)
        {
            Hardwares _hardware = LoadHardwardBoard();

            HardwareProfile _profile = _hardware.Profile.FirstOrDefault(c => c.Name == profileName);

            return _profile;
        }

        public static Programers LoadProgramers()
        {
            Programers _programers = null;

            if (File.Exists(StringConst.PROGRAMERS))
            {
                _programers = XmlSerializable.ReadFile<Programers>(StringConst.PROGRAMERS);
            }

            return _programers;
        }

        public static Programer GetProgramer(string programers)
        {
            Programers _programers = LoadProgramers();

            Programer _programer = _programers.ProgramersList.FirstOrDefault(c => c.Name == programers);

            return _programer;
        }
    }
}
